<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统中心</el-breadcrumb-item>
            <el-breadcrumb-item>科目管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">

        <el-button  type="primary" @click="dialogVisible = true">添加科目</el-button>

            <el-table
                    :data="subjectList"
                    style="width: 100%">

                <el-table-column
                        type="index"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="subjectName"
                        label="科目名称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建时间"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="科目状态"
                        width="180">
                    <template slot-scope="scope">
                        {{scope.row.status|statusFit}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="320">
                    <!--点击按钮 需要绑定@click的时间 例如 @click="add"-->
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="editById(scope.row.id)">修改科目</el-button>
                        <el-button type="primary" size="mini" @click="delById(scope.row.id)">删除科目</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-dialog
                title="添加科目"
                :visible.sync="dialogVisible"
                width="30%">
            <el-form ref="form" :model="subjectForm" label-width="80px" >
                <el-form-item label="科目名称">
                    <el-input v-model="subjectForm.subjectName"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearSubjectSel">取 消</el-button>
                 <el-button type="primary" @click="addSubject">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="编辑科目"
                :visible.sync="dialogVisible1"
                width="30%">
            <el-form ref="form" :model="subjectForm" label-width="80px" >
                <el-form-item label="科目名称">
                    <el-input v-model="subjectForm.subjectName"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearSubjectSel">取 消</el-button>
                 <el-button type="primary" @click="updateSubject">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "ObjectList",
        data(){
            return{
                subjectList:[],
                subjectForm:{},
                dialogVisible:false,
                dialogVisible1:false,
            }
        },
        methods:{
            //科目列表
            subjectSel:function () {
                //debugger;
                this.$http.get('/admin/getSubjectAll').then((res)=>{
                    this.subjectList = res.data.result.subjects
                })
            },
            //逻辑删除
            delById:function (id) {
                this.$http.post('/admin/delSubjectUser?subjectId='+id).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    this.subjectSel();
                })
            },

            //科目添加
            addSubject:function () {
                this.$http.post('/admin/addSubjectUser?subjectName='+this.subjectForm.subjectName).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearSubjectSel();
                })
            },

            //修改回显
            editById:function(id){
                this.dialogVisible1=true
                this.$http.get('/admin/getSubjectInfo?subjectId='+id).then((res)=>{
                    if (res.data.success) {
                       this.subjectForm = res.data.result.subject
                    }else{
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
            },

            //修改科目
            updateSubject:function(id){
                this.$http.post('/admin/setSubjectInfo?subjectId='+this.subjectForm.id+
                    '&subjectName='+this.subjectForm.subjectName).then((res)=>{
                        if (res.data.success) {
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: res.data.message,
                                type: 'error'
                            });
                        }
                        this.clearSubjectSel();
                })
            },

            //清空表单
            clearSubjectSel:function(){
                this.dialogVisible=false
                this.dialogVisible1=false
                this.subjectForm.id=null
                this.subjectForm.subjectName=""
                this.subjectSel()
            }

        },
        filters:{
            statusFit:function (val) {
                if (val == '0'){
                    return "正常运作"
                }
                if (val == '1'){
                    return "已删除"
                }
            }
        },
        mounted() {
            this.subjectSel();
        }

    }
</script>

<style scoped>


</style>
